<!--
 * @Description: 
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 17:21:32
-->
<template>
  <!-- 员工管理页 -->
  <div>
    <!-- 搜索 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="toAdd">添加</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option label="禁用" value="禁用"></el-option>
            <el-option label="正常" value="正常"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div>
      <!-- {{ tableData }} -->
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="编号" width="80" align="center">
        </el-table-column>
        <el-table-column prop="username" label="用户名" align="center">
        </el-table-column>
        <el-table-column prop="realname" label="姓名" align="center">
        </el-table-column
        ><el-table-column prop="telephone" label="手机号" align="center">
        </el-table-column
        ><el-table-column label="性别" align="center">
          <template slot-scope="scope">
            <span
              v-if="scope.row.gender === 'male'"
              class="el-icon-male"
              style="color: #70c9e3"
            >
            </span>
            <span
              v-else-if="scope.row.gender === 'female'"
              class="el-icon-female"
              style="color: #e67c97"
            >
            </span>
            <span v-else>暂无</span>
          </template>
        </el-table-column>
        <el-table-column label="status" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '正常'" type="success"
              >正常</el-tag
            >
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="toDel(scope.row.id)"
              style="color: #e65d6e"
              >删除</el-button
            >
            <el-button
              type="text"
              @click="toDeta(scope.row.id)"
              style="color: #3a71a8"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加/编辑模态框 -->
    <div>
      <el-dialog
        :title="title"
        :visible.sync="addEditVisible"
        width="40%"
        @close="toClose"
      >
        <!-- {{ form }} -->
        <el-form
          :model="form"
          :rules="rules"
          ref="form"
          label-width="80px"
          class="demo-ruleForm"
          :message="message"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="realname">
            <el-input v-model="form.realname"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="telephone">
            <el-input v-model="form.telephone"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              type="password"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="form.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="message === '编辑'" label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="禁用"></el-radio>
              <el-radio label="正常"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addEditVisible = false">取 消</el-button>
          <el-button type="primary" @click="toAddEditSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 删除模态框 -->
    <div>
      <el-dialog
        title="提示"
        :visible.sync="delVisible"
        width="20%"
        :message="message"
      >
        <span>此操作将永久删除该数据, 是否继续??</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="delVisible = false">取 消</el-button>
          <el-button type="primary" @click="delSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  pageQuery as pageQueryEmployee,
  addUserWidthRole,
  saveOrUpdate,
  deleteById,
} from "@/api/user";
export default {
  data() {
    return {
      // 搜索表单的数据
      searchForm: {},
      // 表格数据
      tableData: {},
      params: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      // 员工id
      waiterId: "",
      // 条数
      total: "",
      // 模态框标题
      title: "",
      // 添加/编辑模态框显示
      addEditVisible: false,
      // 删除模态框显示
      delVisible: false,
      // 添加/编辑模态框的数据
      form: {},
      // 提示
      message: "",
      // 表单验证
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        telephone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入秘密", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  computed: {},
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.pageQueryEmployeeData();
      },
      deep: true,
    },
  },
  methods: {
    // 获取员工信息
    async pageQueryEmployeeData() {
      let res = await pageQueryEmployee(this.params);
      // console.log(res);
      this.total = res.data.total;
      this.tableData = res.data.list;
    },
    // 查询按钮点击处理事件
    async toQuery() {
      let temp = { ...this.params, ...this.searchForm };
      for (let key in temp) {
        if (!temp[key]) delete temp[key];
      }
      let res = await pageQueryEmployee(temp);
      this.tableData = res.data.list;
    },
    // 每页显示条数发生变化时
    handleSizeChange(val) {
      this.params = {
        page: 1,
        pageSize: val,
        roleId: 1,
      };
    },
    // 页数发生变化时
    handleCurrentChange(val) {
      this.params.page = val;
    },
    // 重置表单验证
    toClose() {
      this.$refs.form.resetFields();
    },
    // 添加按钮点击处理事件
    toAdd() {
      this.title = "添加员工信息";
      this.form = { roleId: 1 };
      this.message = "添加";
      this.addEditVisible = true;
    },
    // 添加/编辑模态框确定按钮点击处理事件
    toAddEditSave() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.message == "添加") {
            let res = await addUserWidthRole(this.form);
            // console.log(res);
            if (res.status == 200) {
              this.$notify({
                title: "成功",
                message: res.message,
                type: "success",
              });
            }
          } else {
            let res = await saveOrUpdate(this.form);
            // console.log(res);
            if (res.status == 200) {
              this.$notify({
                title: "成功",
                message: res.message,
                type: "success",
              });
            }
          }
          this.pageQueryEmployeeData();
          this.toClose();
          this.addEditVisible = false;
        } else {
          return false;
        }
      });
    },
    // 编辑按钮点击处理事件
    toEdit(row) {
      this.title = "编辑员工信息";
      this.form = { ...row };
      this.message = "编辑";
      this.addEditVisible = true;
    },
    // 删除按钮点击处理事件
    toDel(id) {
      this.waiterId = id;
      this.delVisible = true;
    },
    // 删除模态框的确定按钮点击处理事件
    async delSave() {
      let res = await deleteById({ id: this.waiterId });
      // console.log(res);
      if (res.status == 200) {
        this.$notify({
          title: "成功",
          message: res.message,
          type: "success",
        });
        this.delVisible = false;
        this.pageQueryEmployeeData();
      } else {
        return false;
      }
    },
    toDeta(id) {
      this.$router.push({
        path: "Details",
        query: { id },
      });
    },
  },
  created() {
    this.pageQueryEmployeeData();
  },
  mounted() {},
};
</script>
<style scoped>
.page {
  float: right;
  margin-top: 10px;
}
</style>